package com.xasz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xasz.entity.Activity;
import com.xasz.entity.Jury;
import com.xasz.mapper.ActivityMapper;
import com.xasz.mapper.JuryMapper;
import com.xasz.mapper.UserMapper;
import com.xasz.service.IJuryService;
import com.xasz.service.ex.CreatException;
import com.xasz.service.ex.DeleteException;
import com.xasz.service.ex.NotNxistException;
import com.xasz.service.ex.RepeatException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cai
 * @since 2022-07-28
 */
@AllArgsConstructor
@Service
public class JuryServiceImpl extends ServiceImpl<JuryMapper, Jury> implements IJuryService {

    JuryMapper juryMapper;
    UserMapper userMapper;
    ResultServiceImpl resultService;
    SumServiceImpl sumService;
    ActivityMapper activityMapper;

    @Override
    public void addJury(Jury jury, Integer Uid) {
        addAllJury(jury,Uid);
        //增加评分记录和求和记录
        resultService.creatResult(jury.getActivityId(),Uid);
        sumService.creatSum(jury.getActivityId(),Uid);
    }

    @Override
    public void addAllJury(Jury jury, Integer Uid) {
        //拥有者权限判断
        if (!activityMapper.selectOne(new QueryWrapper<Activity>().eq("id",jury.getActivityId())).getOwner()
                .equals(Uid)){
            throw new DeleteException("你没有权限创建该活动记录表");
        }
        //判断是否有重复评分人
        QueryWrapper<Jury> wrapper=new QueryWrapper<Jury>()
                .eq("activity_id",jury.getActivityId())
                .eq("name",jury.getName());
        if (juryMapper.selectOne(wrapper)!=null){
            throw new RepeatException("已经有重复评委");
        }
        Jury jury1=new Jury();
        jury1.setActivityId(jury.getActivityId());
        jury1.setName(jury.getName());
        if (juryMapper.insert(jury1)!=1){
            throw new CreatException("加入评委时出现错误");
        }
    }

    @Override
    public void detectJury(Integer activityId, Integer juryId, Integer Uid,String username) {
        //拥有者权限判断
        if (!activityMapper.selectOne(new QueryWrapper<Activity>().eq("id",activityId)).getOwner()
                .equals(Uid)){
            throw new DeleteException("你没有权限删除该活动评委");
        }
        //判断是否有评分人
        QueryWrapper<Jury> wrapper=new QueryWrapper<Jury>()
                .eq("activity_id",activityId)
                .eq("id",juryId);
        if (juryMapper.selectOne(wrapper)==null){
            throw new NotNxistException("评委不存在");
        }
        //更新求和记录
        sumService.upDataSum(activityId,Uid,username);

        //删除评分记录
        resultService.detectResultByJury(activityId,juryId,Uid);

        //删除评委
        Jury jury=new Jury();
        jury.setId(juryId);
        jury.setActivityId(activityId);
        if (juryMapper.deleteById(jury)!=1){
            throw new DeleteException("删除评委时出现错误");
        }
    }

    @Override
    public List<Jury> selectByAid(Integer Aid, Integer Uid) {
        if (juryMapper.selectOne(new QueryWrapper<Jury>().eq("activity_id",Aid).eq("name",Uid))==null
        && !activityMapper.selectOne(new QueryWrapper<Activity>().eq("id",Aid)).getOwner().equals(Uid)){
            throw new DeleteException("你没有权限查看该活动评委信息");
        }
        return juryMapper.selectList(new QueryWrapper<Jury>().eq("activity_id",Aid));
    }

}
